//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ForwardRateAgreement.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/InterestRate.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Forward.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (DateTime valueDate, DateTime maturityDate, QL::Position::TypeEnum type, Double strikeForwardRate, Double notionalAmount, Cephei::QL::Indexes::IIborIndex^ index, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Cephei::QL::IPricingEngine^ QL_Pricer) : CForward(CForwardRateAgreement::typeid)
{
    CIborIndex^ _Cindex;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phForwardRateAgreement = NULL;
#endif
        QuantLib::Date _valueDate = (QuantLib::Date)ValueHelper::Convert (valueDate); //d
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //d
        QuantLib::Position::Type _type = (QuantLib::Position::Type)type ;
        QuantLib::Rate _strikeForwardRate = (QuantLib::Rate)ValueHelper::Convert (strikeForwardRate); //d
        QuantLib::Real _notionalAmount = (QuantLib::Real)ValueHelper::Convert (notionalAmount); //d
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve))
        {
            _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve->Value);
            _CdiscountCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppForwardRateAgreement = new boost::shared_ptr<QuantLib::ForwardRateAgreement> (new QuantLib::ForwardRateAgreement ( _valueDate,  _maturityDate,  _type,  _strikeForwardRate,  _notionalAmount,  _index,  _discountCurve ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppForwardRateAgreement)->setPricingEngine (_QL_Pricer);
        SetForward (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (boost::shared_ptr<QuantLib::ForwardRateAgreement>& childNative, Object^ owner) : CForward(CForwardRateAgreement::typeid)
{
#ifdef HANDLE
	_phForwardRateAgreement = NULL;
#endif
	_ppForwardRateAgreement = &childNative;
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
}
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (QuantLib::ForwardRateAgreement& childNative, Object^ owner) : CForward(CForwardRateAgreement::typeid)
{
#ifdef HANDLE
	_phForwardRateAgreement = NULL;
#endif
	_ppForwardRateAgreement = new boost::shared_ptr<QuantLib::ForwardRateAgreement> (&childNative);
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
    _ForwardRateAgreementOwner = owner;
    _ForwardOwner = owner;
}

Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (CForwardRateAgreement^ copy) : CForward(CForwardRateAgreement::typeid)
{
#ifdef HANDLE
	_phForwardRateAgreement = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardRateAgreement = new boost::shared_ptr<QuantLib::ForwardRateAgreement> (copy->GetShared());
        _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
    }
}
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (PLATFORM::Type^ t) : CForward(CForwardRateAgreement::typeid)
{
#ifdef HANDLE
	_phForwardRateAgreement = NULL;
#endif
	if (!t->IsSubclassOf(CForwardRateAgreement::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (QuantLib::Handle<QuantLib::ForwardRateAgreement>& childNative, Object^ owner)  : CForward(CForwardRateAgreement::typeid)
{
	_phForwardRateAgreement = &childNative;
	_ppForwardRateAgreement = &static_cast<boost::shared_ptr<QuantLib::ForwardRateAgreement>>(childNative.currentLink());
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
    _ForwardRateAgreementOwner = owner;
}
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (QuantLib::Handle<QuantLib::ForwardRateAgreement> childNative)  : CForward(CForwardRateAgreement::typeid)
{
	_phForwardRateAgreement = &childNative;
	_ppForwardRateAgreement = &static_cast<boost::shared_ptr<QuantLib::ForwardRateAgreement>>(childNative.currentLink());
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CForwardRateAgreement::CForwardRateAgreement (QuantLib::ForwardRateAgreement childNative)  : CForward(CForwardRateAgreement::typeid)
{
#ifdef HANDLE
	_phForwardRateAgreement = NULL;
#endif
	_ppForwardRateAgreement = new boost::shared_ptr<QuantLib::ForwardRateAgreement> (new QuantLib::ForwardRateAgreement (childNative));
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppForwardRateAgreement));
}
#endif

Cephei::QL::Instruments::CForwardRateAgreement::~CForwardRateAgreement ()
{
    if (_ppForwardRateAgreement != NULL)
    {
	    delete _ppForwardRateAgreement;
        _ppForwardRateAgreement = NULL;
    }
}
Cephei::QL::Instruments::CForwardRateAgreement::!CForwardRateAgreement ()
{
    if (_ppForwardRateAgreement != NULL)
    {
	    delete _ppForwardRateAgreement;
    }
}
QuantLib::ForwardRateAgreement& Cephei::QL::Instruments::CForwardRateAgreement::GetReference ()
{
    if (_ppForwardRateAgreement == NULL) throw REFNEW NativeNullException ();
	return **_ppForwardRateAgreement;
}
boost::shared_ptr<QuantLib::ForwardRateAgreement>& Cephei::QL::Instruments::CForwardRateAgreement::GetShared ()
{
    if (_ppForwardRateAgreement == NULL) throw REFNEW NativeNullException ();
	return *_ppForwardRateAgreement;
}
QuantLib::ForwardRateAgreement* Cephei::QL::Instruments::CForwardRateAgreement::GetPointer ()
{
    if (_ppForwardRateAgreement == NULL) throw REFNEW NativeNullException ();
	return &**_ppForwardRateAgreement;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardRateAgreement>& Cephei::QL::Instruments::CForwardRateAgreement::GetHandle ()
{
	if (_phForwardRateAgreement == NULL)
	{
		_phForwardRateAgreement = new Handle<QuantLib::ForwardRateAgreement> (*_ppForwardRateAgreement);
	}
	return *_phForwardRateAgreement;
}
#endif
bool Cephei::QL::Instruments::CForwardRateAgreement::HasNative () 
{
	return (_ppForwardRateAgreement != NULL);
}

Cephei::QL::IInterestRate^ Cephei::QL::Instruments::CForwardRateAgreement::ForwardRate::get ()
{
    try
    {
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppForwardRateAgreement)->forwardRate ( );   
        Cephei::QL::CInterestRate^ _nrv = REFNEW Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CForwardRateAgreement::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppForwardRateAgreement)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CForwardRateAgreement::SettlementDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardRateAgreement)->settlementDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CForwardRateAgreement::SpotIncome (Cephei::QL::Termstructures::IYieldTermStructure^ incomeDiscountCurve)
{
    CYieldTermStructure^ _CincomeDiscountCurve;
    try
    {
        _CincomeDiscountCurve = safe_cast<CYieldTermStructure^> (incomeDiscountCurve);
        _CincomeDiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _incomeDiscountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CincomeDiscountCurve->GetHandle ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForwardRateAgreement)->spotIncome ( _incomeDiscountCurve );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CincomeDiscountCurve != nullptr) _CincomeDiscountCurve->Unlock();
    }
}
Double Cephei::QL::Instruments::CForwardRateAgreement::SpotValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForwardRateAgreement)->spotValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IForwardRateAgreement^ Cephei::QL::Instruments::CForwardRateAgreement_Factory::Create (DateTime valueDate, DateTime maturityDate, QL::Position::TypeEnum type, Double strikeForwardRate, Double notionalAmount, Cephei::QL::Indexes::IIborIndex^ index, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CForwardRateAgreement ( valueDate,  maturityDate,  type,  strikeForwardRate,  notionalAmount,  index,  discountCurve,  QL_Pricer);
}
